package operacionesWebAPI;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class LlamadasAPI {
	private static String miID = "76561197997499174";
	private static String key = "0DC019442063EB9914488A0AF8ABE9D9";

	public static void ultimas100Partidas() {
		URL oracle;
		try {
			oracle = new URL(
					"https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/V001/?key="
							+ key + "&account_id=" + miID);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					oracle.openStream()));

			FileWriter fstream = new FileWriter("ultimas100");
			BufferedWriter out = new BufferedWriter(fstream);

			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				out.write(inputLine);
			}

			in.close();
			out.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void siguientes100Partidas(long ultimoMatch) {
		try {
			URL oracle = new URL(
					"https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/V001/?key="
							+ key + "&start_at_match_id=" + ultimoMatch
							+ "&account_id=" + miID);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					oracle.openStream()));

			FileWriter fstream = new FileWriter("ultimas100");
			BufferedWriter out = new BufferedWriter(fstream);

			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				out.write(inputLine);
			}

			in.close();
			out.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void siguientes100PartidasStartTime(long startTime) {
		try {
			URL oracle = new URL(
					"https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/V001/?key="
							+ key + "&date_max=" + startTime + "&account_id="
							+ miID);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					oracle.openStream()));

			FileWriter fstream = new FileWriter("ultimas100");
			BufferedWriter out = new BufferedWriter(fstream);

			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				out.write(inputLine);
			}

			in.close();
			out.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void siguientes100PartidasSequence(long matchSequence) {
		System.out.println("EMPEZAMOS DESDE " + matchSequence);
		
		
		try {
			URL oracle = new URL(
					"https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/V001/?key="
							+ key + "&start_at_match_seq_num=" + matchSequence
							+ "&account_id=" + miID);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					oracle.openStream()));

			FileWriter fstream = new FileWriter("ultimas100");
			BufferedWriter out = new BufferedWriter(fstream);

			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				out.write(inputLine);
			}

			in.close();
			out.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void informacionPartida(long matchID) {
		try {
			URL oracle = new URL(
					"https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?key="
							+ key + "&match_id=" + matchID);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					oracle.openStream()));

			FileWriter fstream = new FileWriter("infoPartida");
			BufferedWriter out = new BufferedWriter(fstream);

			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				out.write(inputLine);
			}

			in.close();
			out.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void getHeroesAPI() {
		try {
			URL oracle = new URL(
					"https://api.steampowered.com/IEconDOTA2_570/GetHeroes/v0001/?key="
							+ key + "&language=en_us");
			BufferedReader in = new BufferedReader(new InputStreamReader(
					oracle.openStream()));

			FileWriter fstream = new FileWriter("heroes");
			BufferedWriter out = new BufferedWriter(fstream);

			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				out.write(inputLine);
			}

			in.close();
			out.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void getItemsAPI() {
		try {
			URL oracle = new URL("http://www.dota2.com/jsfeed/itemdata");
			BufferedReader in = new BufferedReader(new InputStreamReader(
					oracle.openStream()));

			FileWriter fstream = new FileWriter("items");
			BufferedWriter out = new BufferedWriter(fstream);

			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				out.write(inputLine);
			}

			in.close();
			out.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
